package routinecontroller

import (
	"agent-list---web-development/list/common"
	"agent-list---web-development/list/model"
	"agent-list---web-development/list/response"
	"net/http"

	"github.com/gin-gonic/gin"
)

func DeleteRoutine(c *gin.Context) {
	//获取参数
	DB := common.DB
	title := c.Query("title")
	account := c.Query("account")
	//数据验证
	if len(title) == 0 {
		response.Response(c, http.StatusUnprocessableEntity, 422, nil, "待删除打卡名不能为空！")
		return
	}
	var user = model.User{}
	DB.Where("account=? ", account).First(&user)
	if user.Id == 0 {
		response.Response(c, http.StatusUnprocessableEntity, 422, nil, "该用户不存在！")
		return
	}
	//删除
	var routine model.Routine
	DB.Where("useraccount=? and title=?", account, title).First(&routine)
	if routine.Id == 0 {
		response.Response(c, http.StatusUnprocessableEntity, 422, nil, "该打卡项目不存在！")
		return
	}
	DB.Exec("DELETE FROM routines WHERE useraccount=? and title=? ", account, title)
	//DB.Delete(routine)
	response.Success(c, nil, "删除打卡成功！")
}
